// pages/edit-team/edit-team.js
import {
    getusermanageprojectapi
} from '../api/personalMsg.js'
import {
    delMember,
    upMemberPosition,
    coSeatInfo,
    setseatadminapi
} from '../../api/app.js'
import {
    shake,
    toast,
    confirm,
} from '../../utils/util.js'
import user from '../../utils/user.js'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isapprove: '',
        disabled: false,
        seatInfo: {},
        isuptype: 1,
        pcrole: {
            label: '',
            value: ''
        },
        isseatadmin: {
            label: '',
            value: ''
        },
        minrole: {
            label: '',
            value: ''
        },
        isup: false,
        isup2: false,
        position: '',
        platformlist: [{
                text: '小程序',
                isselect: false,
                type: 1
            },
            {
                text: '电脑端',
                isselect: false,
                type: 2
            }
        ],
        gradeArr: [{
                img: 'https://bullet001.oss-cn-shanghai.aliyuncs.com/file/payManager5139e2cd-0e89-4947-a4c2-2d0205c4e0bc.png',
                title: '普通成员',
                msg: '在客户项目现场辅助管理的人员',
                isselect: false,
                value: '0'
            },
            {
                img: 'https://bullet001.oss-cn-shanghai.aliyuncs.com/file/payManagerb4eafcb0-715c-479b-be59-7da7538a3b4d.png',
                title: '系统管理员',
                msg: '可管理公司所有成员、系统设置等',
                isselect: false,
                value: '1'
            },
        ],
        grade: '',
        id: '',
        isAdmin: '',
        name: '',
        isSetState: false,
        isSuperAdmin: '',
        isPcAdmin: '', //pc端角色
        pcGrade: '',
        phone: '',
        pagesize: 10,
        pagenum: 1,
        innerhasnext: true,
        innerlist: [],
        total: 0
    },
    changeJobName(e) {
        this.setData({
            position: e
        })
        setTimeout(() => {
            console.log(e, 'uhsduygdfuyh');
            this.onUpMemberPosition()
        }, 100);
    },
    toEditJob() {
        console.log(user.isSuperAdmin);
        if (user.isSuperAdmin) {
            wx.navigateTo({
                url: '/pages/edit-job/edit-job?position=' + this.data.position,
                events: {
                    openerData: (data) => {
                        this.changeJobName(data)
                    }
                },
            })
        }
    },
    async getusermanageproject() {
        let res = await getusermanageprojectapi({
            userId: this.data.id,
            pageSize: this.data.pagesize,
            pageNum: this.data.pagenum,
        })
        console.log(res, 'sudghuysuyg');
        this.setData({
            innerlist: this.data.innerlist.concat(res.list),
            innerhasnext: res.hasNextPage,
            total: res.totalCount
        })
    },
    init() {
        this.setData({
            gradeArr: [{
                    img: 'https://bullet001.oss-cn-shanghai.aliyuncs.com/file/payManager5139e2cd-0e89-4947-a4c2-2d0205c4e0bc.png',
                    title: '普通成员',
                    msg: '在客户项目现场辅助管理的人员',
                    isselect: false,
                    value: '0'
                },
                {
                    img: 'https://bullet001.oss-cn-shanghai.aliyuncs.com/file/payManagerb4eafcb0-715c-479b-be59-7da7538a3b4d.png',
                    title: '系统管理员',
                    msg: '可管理公司所有成员、系统设置等',
                    isselect: false,
                    value: '1'
                },
            ],
            platformlist: [{
                    text: '小程序',
                    isselect: false,
                    type: 1
                },
                {
                    text: '电脑端',
                    isselect: false,
                    type: 2
                }
            ],
        })
    },
    openpoipup(e) {
        this.init()
        console.log(e.currentTarget.dataset.type, !this.data.disabled);
        this.data.gradeArr.forEach((ele, i) => {
            if (ele.value === e.currentTarget.dataset.value) {
                ele.isselect = true
            }
        })
        if ((e.currentTarget.dataset.type === 1 || e.currentTarget.dataset.type === 2) && this.data.disabled) {
            console.log(999);
            return
        }
        if (e.currentTarget.dataset.type === 1) {
            this.setData({
                gradeArr: this.data.gradeArr,
                platformlist: [{
                        text: '小程序',
                        isselect: true,
                        type: 1
                    },
                    {
                        text: '电脑端',
                        isselect: false,
                        type: 2
                    }
                ],
            })
        } else if (e.currentTarget.dataset.type === 2) {
            this.setData({
                gradeArr: this.data.gradeArr,
                platformlist: [{
                        text: '小程序',
                        isselect: false,
                        type: 1
                    },
                    {
                        text: '电脑端',
                        isselect: true,
                        type: 2
                    }
                ],
            })
        } else {
            this.coSeatInfo()
        }
        this.setData({
            isuptype: e.currentTarget.dataset.type,
            isup: true
        })
    },
    onChangeSeat(e) {
        console.log(e);
        if (e.detail.value) {
            this.setData({
                isseatadmin: {
                    label: '已分配',
                    value: true
                }
            })
            this.setSeat()
        } else {
            if (this.data.isapprove) { // 在审批流
                if (this.data.isseatadmin.value) {
                    confirm(`该员工为发薪项目审批人，需超级管理员取消其审批人身份后，才可取消席位`, {
                            confirmText: user.isSuperAdmin ? '进审批流' : '知道了',
                            showCancel: user.isSuperAdmin,
                            confirmColor: '#00D679'
                        })
                        .then(res => {
                            this.setData({
                                isseatadmin: {
                                    label: '已分配',
                                    value: true
                                }
                            })
                            if (user.isSuperAdmin) {
                                wx.navigateTo({
                                    url: '/pages/flow/index/index'
                                })
                            } else {
                                // this.setSeat()
                            }
                        }).catch(err => {
                            this.setData({
                                isseatadmin: {
                                    label: '已分配',
                                    value: true
                                }
                            })
                            // this.setSeat()
                        })
                } else {
                    this.setSeat()
                }
            } else { // 不在审批流
                this.setData({
                    isseatadmin: {
                        label: '未分配',
                        value: false
                    }
                })
                this.setSeat()
            }
        }
    },
    setSeat() {
        setseatadminapi({
            userId: this.data.id,
            isSeatAdmin: this.data.isseatadmin.value
        }).then((res) => {
            console.log(res);
            if (res.msg === "发薪席位数量不足") {
                this.setData({
                    isup2: true,
                    isseatadmin: {
                        label: '未分配',
                        value: false
                    }
                })
            } else {
                wx.showToast({
                    title: '修改成功',
                    icon: 'none'
                })
            }
            this.setData({
                isup: false
            })
        })
    },
    // 获取席位信息
    coSeatInfo() {
        coSeatInfo({}).then(res => {
            this.setData({
                seatInfo: res || {}
            })
        })
    },
    closepopup() {
        console.log(11);
        this.setData({
            isup: false
        })
    },
    closepopup2() {
        console.log(11);
        this.setData({
            isup2: false
        })
    },
    changerole(e) {
        this.data.gradeArr.forEach((ele, i) => {
            if (i === e.currentTarget.dataset.i) {
                ele.isselect = true
            } else {
                ele.isselect = false
            }
        })
        this.setData({
            gradeArr: this.data.gradeArr
        })
    },
    surechangerole() {
        let value = ''
        this.data.gradeArr.forEach((ele, i) => {
            if (ele.isselect) {
                value = ele.value
            }
        })
        if (this.data.platformlist[0].isselect && !this.data.platformlist[1].isselect) {
            this.setData({
                minrole: {
                    label: value === '1' ? '管理员' : '员工',
                    value: value
                },
            })
        } else if (!this.data.platformlist[0].isselect && this.data.platformlist[1].isselect) {
            this.setData({
                pcrole: {
                    label: value === '1' ? '管理员' : '员工',
                    value: value
                },
            })
        } else {
            this.setData({
                pcrole: {
                    label: value === '1' ? '管理员' : '普通成员',
                    value: value
                },
                minrole: {
                    label: value === '1' ? '管理员' : '普通成员',
                    value: value
                },
            })
        }
        this.onUpMemberPosition()
        this.setData({
            isup: false
        })
    },
    changeselect(e) {
        this.data.platformlist.forEach((ele, i) => {
            if (i === e.currentTarget.dataset.i) {
                ele.isselect = !ele.isselect
            }
        })
        this.setData({
            platformlist: this.data.platformlist
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        user.waitLogin().then(res => {
            if (user.isSuperAdmin) {
                console.log(999);
                this.setData({
                    disabled: false
                })
            } else if (options.isAdmin === '1' && user.isAdmin) {
                console.log(888);
                this.setData({
                    disabled: true
                })
            }
        })
        console.log(options, 'options')
        this.setData({
            isapprove: options.isapprove === 'true' ? true : false,
            id: options.id,
            isAdmin: options.isAdmin,
            name: options.name,
            position: options.position,
            grade: options.isAdmin == '0' ? '员工' : '管理员',
            isSetAdminState: options.isAdmin == 2 ? true : !user.isSuperAdmin,
            isSuperAdmin: !user.isSuperAdmin,
            isPcAdmin: options.isPcAdmin,
            pcGrade: (options.isPcAdmin == '0' || options.isPcAdmin == 'undefined') ? '员工' : '管理员',
            phone: options.phone,
            pcrole: {
                label: (options.isPcAdmin == '0' || options.isPcAdmin == 'undefined') ? '员工' : '管理员',
                value: options.isPcAdmin
            },
            minrole: {
                label: options.isAdmin == '0' ? '员工' : '管理员',
                value: options.isAdmin
            },
            isseatadmin: {
                label: options.isseatadmin == '1' ? '已分配' : '未分配',
                value: options.isseatadmin == '1' ? true : false
            }
        })
        this.getusermanageproject()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    // 切换身份
    onSetGrade(event) {
        console.log(event)
        const {
            value
        } = event.detail
        this.setData({
            grade: this.data.gradeArr[value],
            isAdmin: value
        })
    },
    // 设置pc端角色
    onSetPcGrade(event) {
        const {
            value
        } = event.detail
        this.setData({
            pcGrade: this.data.gradeArr[value],
            isPcAdmin: value
        })
    },
    // 删除员工
    deleteT(event) {
        if (user.isSuperAdmin || (user.isAdmin && this.data.isAdmin == 0)) {
            app.confirm('是否删除该员工？').then(() => {
                delMember({
                    memberId: this.data.id,
                }).then(() => {
                    app.toast('删除成功！', 'success')
                    wx.navigateBack()
                })
            })
        } else {
            app.toast('暂无权限修改')
            return
        }

    },
    onReachBottom() {
        if (this.data.innerhasnext) {
            this.data.pagenum += 1
            this.getusermanageproject()
        }
    },
    back() {
        this.setData({
            isup2: false
        })
        wx.navigateBack()
    },
    tobuy() {
        this.setData({
            isup2: false
        })
        wx.navigateTo({
            url: '/packageInsurance/pages/insurance-recharge-bullet/insurance-recharge-bullet'
        })
    },
    // 修改团队成员 权限和 职位
    onUpMemberPosition() {
        console.log('hudfsghuyhuy111222');
        // app.confirm('确定修改').then(() => {
        upMemberPosition({
            userId: this.data.id,
            position: this.data.position,
            isAdmin: this.data.minrole.value,
            isPcAdmin: this.data.pcrole.value,
        }).then((res) => {
            console.log(res, 'dsiguh');
            if (res.msg === "修改成功") {
                setTimeout(() => {
                    app.toast('修改成功', 'success')
                    // wx.navigateBack()
                }, 100);
            }
        })
        // })
    }
})